﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace medder
{
    public partial class EditUser : Form
    {
        User user;
        MedBaseEntities med = new MedBaseEntities();
        public static ObjectSet<User> users;


        public EditUser(string passport)
        {
            InitializeComponent();
            users = med.Users;
            user = users.Where(x => x.Passport == passport).FirstOrDefault();
           // this.user = user;
            
        }


        private void button1_Click(object sender, EventArgs e)
        {
            string login = TBLogin.Text;

            string password = TBPassword.Text;
            if (CheckPassword(password))
            {

                string name = TBName.Text;
                if (!Valid.IsOnlyLetters(name))
                {
                    ShowError("имени");
                    TBName.Focus();
                }
                string surname = TBSurname.Text;
                if (!Valid.IsOnlyLetters(surname))
                {
                    ShowError("фамилии");
                    TBSurname.Focus();
                }
                string patronymic = TBPatr.Text;
                if (!Valid.IsOnlyLetters(patronymic))
                {
                    ShowError("отчества");
                    TBPatr.Focus();
                }

                string passport = MTBPassport.Text;
                if (Valid.CheckPassport(passport, Info2) || (passport == user.Passport))
                {
                    user.Login = login;
                    user.Password = password;
                    user.Name = name;
                    user.Surname = surname;
                    user.Patronymic = patronymic;
                    user.Passport = passport;
                    user.IsActive = isActive.Checked;

                    if (button1.Enabled)
                    {
                        
                        med.SaveChanges();
                        this.Close();

                    }
                }
            }


        }

        private bool CheckLogin(string login)
        {
            login = login.Trim();

            if (login.Equals(string.Empty))
            {
                Info1.Text = "Укажите логин";
                return false;
            }
            if (users.Where(x => x.Login == login).Count() == 0)
                return true;
            else
            {
                Info1.Text = "Таком пользователь уже существует";
                return false;
            }
        }

        private bool CheckPassword(string password)
        {
            if (password.Length > 4)
            {
                Info1.Text = string.Empty;
                return true;
            }
            else
            {
                Info1.Text = "Пароль должен быть не менее 4 символов";
                return false;
            }

        }

        private void ShowError(string message)
        {
            Info2.Text = "Проверьте корректность ввода " + message;
            button1.Enabled = false;
        }

        private void TBSurname_TextChanged(object sender, EventArgs e)
        {
            if (!Valid.IsOnlyLetters(((TextBox)sender).Text))
                ShowError("имени");
            else
            {
                button1.Enabled = true;
                Info2.Text = String.Empty;
            }
        }

        private void TBName_TextChanged(object sender, EventArgs e)
        {
            if (!Valid.IsOnlyLetters(((TextBox)sender).Text))
                ShowError("фамилии");
            else
            {
                button1.Enabled = true;
                Info2.Text = String.Empty;
            }
        }

        private void TBPatr_TextChanged(object sender, EventArgs e)
        {
            if (!Valid.IsOnlyLetters(((TextBox)sender).Text))
                ShowError("отчества");
            else
            {
                button1.Enabled = true;
                Info2.Text = String.Empty;
            }
        }

        private void Sign_Load(object sender, EventArgs e)
        {
            TBLogin.Text = user.Login;
            TBPassword.Text = user.Password;

            TBName.Text = user.Name;
            TBSurname.Text = user.Surname;
            TBPatr.Text = user.Patronymic;
            MTBPassport.Text = user.Passport;
            isActive.Checked = user.IsActive == null ? false : (bool)user.IsActive;


        }
    }
}
